package com.android.dfsc;

import com.android.dfsc.data.Hris1000System;
import com.android.dfsc.data.HrisDeviceInfo;

import android.app.Application;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

public class GSmartHome extends Application {
	
	public final static int SCENARIO_AWAY = 1;
	public final static int SCENARIO_HOME = 2;
	
	SmartHomeEngine instance = null;
	static Hris1000System h1000Data;

	String getMac(){
		String macAddress="";
		WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
		if (wifiManager != null )
		{
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			if (wifiInfo != null)
			macAddress=wifiInfo.getMacAddress();
			Log.d("honey-TEST", "IP address is:" + intToIp(wifiInfo.getIpAddress()));;
		}
		return macAddress;
		
	}
	private String intToIp(int i)
	 {
	 return ( i & 0xFF)+ "." + ((i >> 8 ) & 0xFF)  + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF ) ;
	 }

	static void regetData() {
		h1000Data = SmartHomeEngine.getInstance().getAppData();
		h1000Data.setAreaList(SmartHomeEngine.getInstance().getAreaData());
		if (h1000Data.getAreaList().length > 1)
			h1000Data.setActiveArea(h1000Data.getAreaList()[1].getId());
		// else
		// h1000Data.setActiveArea(h1000Data.getAreaList()[0].getId());
		// h1000Data.setDeviceList(instance.getDeviceData());
		h1000Data.setDeviceList(new HrisDeviceInfo());
		h1000Data.getDeviceList().setLightList(
				SmartHomeEngine.getInstance().getLightData());
		h1000Data.getDeviceList().setCurtainList(
				SmartHomeEngine.getInstance().getCurtainData());
		h1000Data.getDeviceList().setUFHList(
				SmartHomeEngine.getInstance().getUFHData());
		h1000Data.getDeviceList().setRelayList(
				SmartHomeEngine.getInstance().getRelayData());
		
		h1000Data.getDeviceList().setIracList(
				SmartHomeEngine.getInstance().getIracData());
		for (int i=0;i<h1000Data.getDeviceList().getIracList().length;i++){
			h1000Data.getDeviceList().getIracList()[i].setMode(SmartHomeEngine.getInstance().getIracModeData(i));
		}
		
		
		h1000Data.getDeviceList().setIrdevList(
				SmartHomeEngine.getInstance().getIrdevData());
		for (int i=0;i<h1000Data.getDeviceList().getIrdevList().length;i++){
			h1000Data.getDeviceList().getIrdevList()[i].setMode(SmartHomeEngine.getInstance().getIrdevModeData(i));
		}
		
		h1000Data.setScenarioList(SmartHomeEngine.getInstance()
				.getScenarioData());
		h1000Data.setFastScenarioList(SmartHomeEngine.getInstance()
				.getFastScenarioData());
		if (h1000Data.getScenarioList().length > 1)
			h1000Data.setActiveScenario(h1000Data.getScenarioList()[0].getId());
		h1000Data.setZoneList(SmartHomeEngine.getInstance().getZoneData());
		h1000Data.setWirelessZoneList(SmartHomeEngine.getInstance()
				.getWirelessZoneData());
	}

	public GSmartHome() {
		// TODO Auto-generated constructor stub
		instance = SmartHomeEngine.getInstance();
	}

	

	@Override
	public void onCreate() {
		super.onCreate();
		
		
		//macAddress="00.11.22.33.44.55";
		
		instance.setAuthCode(instance.getAuthCode(this));
		
		instance.setContext(this);
		instance.setConfig();
		instance.initEngine();
		
		/*SharedPreferences   clientConfig = getSharedPreferences("client_config",0);
		//syncConfig("1","2","3","4","5");
		String ipStr=clientConfig.getString("IP","");
		String userNameStr=clientConfig.getString("user_name","");
		String pwdStr=clientConfig.getString("password","");
		String authCodeStr=instance.getAuthCode();
		String authStr=clientConfig.getString("auth_code","");
		instance.syncConfig(ipStr,userNameStr,pwdStr,authCodeStr,authStr);*/
		//instance.setWifiMacAddress(getWifiMacAddress());
		regetData();
		//instance.setConfig();
//		h1000Data = instance.getAppData();
//		h1000Data.setAreaList(instance.getAreaData());
//		if (h1000Data.getAreaList().length > 1)
//			h1000Data.setActiveArea(h1000Data.getAreaList()[1].getId());
//		// else
//		// h1000Data.setActiveArea(h1000Data.getAreaList()[0].getId());
//		// h1000Data.setDeviceList(instance.getDeviceData());
//		h1000Data.setDeviceList(new HrisDeviceInfo());
//		h1000Data.getDeviceList().setLightList(instance.getLightData());
//		h1000Data.getDeviceList().setCurtainList(instance.getCurtainData());
//		h1000Data.getDeviceList().setUFHList(instance.getUFHData());
//		h1000Data.getDeviceList().setRelayList(instance.getRelayData());
//		h1000Data.getDeviceList().setIrdevList(
//				SmartHomeEngine.getInstance().getIrdevData());
//		for (int i=0;i<h1000Data.getDeviceList().getIrdevList().length;i++){
//			h1000Data.getDeviceList().getIrdevList()[i].setMode(SmartHomeEngine.getInstance().getIrdevModeData(i));
//		}
//		h1000Data.setScenarioList(instance.getScenarioData());
//		if (h1000Data.getScenarioList().length > 1)
//			h1000Data.setActiveScenario(h1000Data.getScenarioList()[0].getId());
//		h1000Data.setZoneList(instance.getZoneData());
//		h1000Data.setWirelessZoneList(instance.getWirelessZoneData());
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		SmartHomeEngine.getInstance().destoryEngine();
	}
}
